using System;
using System.IO;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using AvaloniaMVVMSample.Models;
using AvaloniaMVVMSample.ViewModels;

namespace AvaloniaMVVMSample.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void BackToParentClick(object? sender, RoutedEventArgs e)
    {
        if(DataContext is not MainWindowViewModel model)return;
        var folder = new DirectoryInfo(model.Text);
        if(folder.Parent == null)return;
        model.Text = folder.Parent.FullName;
    }

    private void InputEnterKeyUp(object? sender, KeyEventArgs e)
    {
        if(e.Key != Key.Enter)return;
        if(sender is not TextBox box)return;
        if(DataContext is not MainWindowViewModel model)return;
        if(string.IsNullOrEmpty(box.Text))return;
        model.Text = box.Text;
    }
}